package com.example.sample.activity;

import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;

import com.example.app.BaseActivity;
import com.example.sample.R;

import net.liteheaven.xblib.widget.edittext.FixedLineCountEditText;

public class GestureActivity extends BaseActivity {
	
	private SimpleOnGestureListener mGestureListener = new SimpleOnGestureListener(){
		public boolean onDown(MotionEvent e) {
			log("onDown");
			return false;
		};

		public void onLongPress(MotionEvent e) {
			log("onLongPress");
		};

		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
				float velocityY) {
			log("onFling");
			return false;
		};

		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			log("onScroll");
			return false;
		};

		public void onShowPress(MotionEvent e) {
			log("onShowPress");
		};

		public boolean onSingleTapUp(MotionEvent e) {
			log("onSingleTapUp");
			return false;
		};

		public boolean onSingleTapConfirmed(MotionEvent e) {
			log("onSingleTapConfirmed");
			return false;
		};

		public boolean onDoubleTap(MotionEvent e) {
			log("onDoubleTap");
			return false;
		};

		public boolean onDoubleTapEvent(MotionEvent e) {
			log("onDoubleTapEvent");
			return false;
		};
	};
	
	private GestureDetector mGestureDetector;
	private FixedLineCountEditText mLogEditText;

	protected void onCreate(android.os.Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gesture);
		mGestureDetector = new GestureDetector(this, mGestureListener);
		mLogEditText = (FixedLineCountEditText)findViewById(R.id.logEdit);
		mLogEditText.setMaxLineCount(25);
		mLogEditText.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mLogEditText.setText("");
			}
		});
	};
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		boolean result = mGestureDetector.onTouchEvent(event);
		if (result){
			result = super.onTouchEvent(event);
		}
		if (event.getAction() != 2){
			String eventString = getMotionEventString(event); 
			log2(eventString);
		}		
		return result;
	}
	
	private void log(String content){
		mLogEditText.appendLine(content);
	}
	
	private void log2(String content){
		mLogEditText.appendLine(content);
	}
	
	private String getMotionEventString(MotionEvent e){
		String ret = "";		
		int action = e.getAction() & MotionEvent.ACTION_MASK;
		String [] actionNameList = {"DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE", "PTR_DOWN", "PTR_UP"};
		String actionName = actionNameList[action];
		ret += actionName;
		ret += "\nACTION_INDEX="+e.getActionIndex()+"\n";
		for (int i = 0; i < e.getPointerCount(); i++) {
			float x = e.getX(i);
			float y = e.getY(i);
			
			ret += "" + i + ">" + "(" + x + "," + y + ")\n";
		}
		return ret;
	}
}
